package cn.fxl.basic.utils;

import cn.fxl.user.domain.Logininfo;
import cn.fxl.user.jwt.JwtUtils;
import cn.fxl.user.jwt.LoginData;
import cn.fxl.user.jwt.Payload;
import cn.fxl.user.jwt.RsaUtils;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * 从request中获取登录信息工具类
 */
public class LoginContext {
    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            String jwtToken  = request.getHeader("token");
            PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
            Payload<LoginData> payload = JwtUtils.getInfoFromToken(jwtToken , publicKey, LoginData.class);
            LoginData loginData = payload.getLoginData();
            return loginData.getLogininfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
